import request from "@/utils/request";
import axios from "axios";

export function testApi() {
  return request({
    url: "/test",
    method: "get",
  });
}

export async function uploadApi(file: File) {
  const ext = file.name.split('.').pop();
  // 1. 获取预签名URL
  const res: any = await request({
    url: `api/minio/upload-url/${ext}`,
    method: "get",
  });

  console.log(res);
  

  // 2. 判断返回
  if (res.url) {
    // 3. 直传文件到 MinIO
    await axios({
      url:res.url,
      method:"put",
      data:file,
      headers:{
        "Content-Type":file.type,
      }
    })
    // 4. 返回 avatar 供后续业务使用
    return { code: 0, data: res.fileUrl };
  } else {
    return { code: 1, data: null };
  }
}
